﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Cards.Util;
using Cards.Objects;

namespace Cards.GUI
{
    public partial class MainFrame : Form
    {
        private INS_Stage thvb;
        public CMultiPlayer sMultiPlayer = new CMultiPlayer();

        public static String sCurDir = "";
        public static Boolean sChoiVanTiep = false;
        public static CARD_PLAYING_KIND sPlayingKind = CARD_PLAYING_KIND.BLACKJACK;
        public static int sNumOfPlayers = 0;
        
        
        public MainFrame()
        {
            InitializeComponent();
            KhoiTaoBien();
            TaoMoi();
        }

        public void KhoiTaoBien()
        {
            sCurDir = Environment.CurrentDirectory;
            this.Size = new Size(new Point(Constants.s_rongBan,
                                           Constants.s_daiBan + menuStrip1.Height));
            this.Location = new Point(0, 0);
            sNumOfPlayers = 4;
        }

        public void KhoiTao()
        {
            switch (sPlayingKind)
            {
                case CARD_PLAYING_KIND.VNPOCKER:
                    thvb = new INS_VNPockerStage(1, 1, Constants.s_rongBan, Constants.s_daiBan, this, sNumOfPlayers);
                    this.Controls.Add(thvb);
                    break;

                case CARD_PLAYING_KIND.BLACKJACK:
                    thvb = new INS_BlackJackStage(1, 1, Constants.s_rongBan, Constants.s_daiBan, this, sNumOfPlayers);
                    this.Controls.Add(thvb);
                    break;

                case CARD_PLAYING_KIND.CATTE:
                    thvb = new INS_CatteStage();
                    this.Controls.Add(thvb);
                    break;

                default:
                    break;
            }
        }

        private void tạoMớiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TaoMoi();
        }

        public void TaoMoi()
        {
            sChoiVanTiep = false;
            if (thvb != null)
            {
                thvb.XoaVanBai();
                this.Controls.Remove(thvb);
                thvb.Dispose();
            }
            KhoiTao();
            thvb.ChoiBai();
        }

        public void ChoiTiep()
        {
            sChoiVanTiep = true;
            thvb.KetThuc();
            thvb.ChoiTiep();
        }

        private void đóngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            ChoiTiep();
        }

        private void bàiTiếnLênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sPlayingKind = CARD_PLAYING_KIND.VNPOCKER;
            vnPockerToolStripMenuItem.Checked = true;
            blackjackToolStripMenuItem.Checked = false;
            catteToolStripMenuItem.Checked = false;
            sNumOfPlayers = 4;
        }

        private void xìJackToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sPlayingKind = CARD_PLAYING_KIND.BLACKJACK;
            vnPockerToolStripMenuItem.Checked = false;
            blackjackToolStripMenuItem.Checked = true;
            catteToolStripMenuItem.Checked = false;
            sNumOfPlayers = 4;
        }

        private void catteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sPlayingKind = CARD_PLAYING_KIND.CATTE;
            vnPockerToolStripMenuItem.Checked = false;
            blackjackToolStripMenuItem.Checked = false;
            catteToolStripMenuItem.Checked = true;
            sNumOfPlayers = 4;
        }
        /// <summary>
        /// Show configuration dialog for playing multiplayer by LAN
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void multiPlayerToolStripMenuItemConfig_Click(object sender, EventArgs e)
        {
            frmConfigurationDialog frmConfig = new frmConfigurationDialog();
            
            frmConfig.iMultiPlayer = sMultiPlayer.MultiPlayer;
            frmConfig.strIPServer = sMultiPlayer.IPServer;
            frmConfig.strPortServer = sMultiPlayer.PortServer;

            if (frmConfig.ShowDialog() == DialogResult.OK)
            {
                sMultiPlayer.PortServer = frmConfig.strPortServer;
                sMultiPlayer.MultiPlayer = frmConfig.iMultiPlayer;
                sMultiPlayer.IPServer = frmConfig.strIPServer;
            }
        }
    }
}